#include <iostream>
#include <queue>
using namespace std;
#define maxn 100005
int main() {
    int n;
    long long a;
    while (cin>>n) {
        priority_queue<long long, vector<long long>, greater<long long > > que;
        for(int i = 0; i < n; i++){
            cin>>a;
            que.emplace(a);
        }
        long long  total = 0;
        long long l1,l2,sum;
        while (que.size() > 1) {
            l1 = que.top();
            que.pop();
            l2 =que.top();
            que.pop();
            sum = (l1+l2);
            total += sum;
            que.push(sum);
        }
        cout<<total<<endl;
    }
}
// 64 位输出请用 printf("%lld")